package com.usian.controller;

import com.usian.feign.ItemFeign;
import com.usian.pojo.TbItem;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("backend/item")
public class ItemController {
    @Autowired
    private ItemFeign itemFeign;
    @RequestMapping("selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer rows){
       PageResult pageResult =itemFeign.selectTbItemAllByPage(page,rows);
       if (pageResult!=null && pageResult.getResult().size()>1){

           return Result.ok(pageResult);
       }
       return Result.error("数据查询失败");
    }

    @RequestMapping("insertTbItem")
    public Result insertTbItem(TbItem tbItem,@RequestParam String desc,@RequestParam String itemParams){
               int num=itemFeign.insertTbItem(tbItem,desc,itemParams);
               if (num==3){
                   return Result.ok();
               }
               return Result.error("添加失败");
    }
    @RequestMapping("deleteItemById")
    public Result deleteItemById(@RequestParam String itemId){
        int num=itemFeign.deleteItemById(itemId);
        if (num==1){
            return Result.ok();
        }
        return null;

    }

    @RequestMapping("preUpdateItem")
    public Result preUpdateItem(@RequestParam Long itemId){
        Map<String,Object>map=itemFeign.preUpdateItem(itemId);
        if (map.size()>0){
            return Result.ok(map);
        }
        return Result.error("回显失败");


    }

    @RequestMapping("updateTbItem")
    public Result updateTbItem(TbItem tbItem,@RequestParam String desc,@RequestParam String itemParams){
        int num=itemFeign.updateTbItem(tbItem,desc,itemParams);
        if (num==3){
            return Result.ok();
        }
        return Result.error("修改失败");
    }

}
